﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using GoogleMapLibrary;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
[ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod(EnableSession = true)]
    public GoogleMapObject GetGoogleMapObject()
    {
        GoogleMapObject objGoogle = (GoogleMapObject)System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT"];
        System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT_OLD"] = new GoogleMapObject(objGoogle);
        return objGoogle;
    }

    [WebMethod(EnableSession = true)]
    public GoogleMapObject GetOptimizedGoogleObject()
    {
        GoogleMapObject objGoogleNew = (GoogleMapObject)System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT"];
        GoogleMapObject objGoogleOld = (GoogleMapObject)System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT_OLD"];
        GoogleMapObject objGoogle = new GoogleMapObject();

        if (objGoogleOld != null)
        {

            for (int i = 0; i < objGoogleNew.Markers.Count; i++)
            {
                string pointStatus = "";
                Marker NewPoint = objGoogleNew.Markers[i];
                Marker OldPoint = objGoogleOld.Markers[NewPoint.ID];
                //if old point not found, means this is a new point.
                if (OldPoint == null)
                {
                    pointStatus = "N"; //New
                }
                else
                {
                    //If old point found and old not equal to new point, means it's value is changed.
                    if (!OldPoint.Equals(NewPoint))
                    {
                        pointStatus = "C"; //Changed
                    }
                    //Remove found point from old object. This is to reduce iteration in next loop.
                    objGoogleOld.Markers.Remove(OldPoint.ID);
            
                }
                if (pointStatus != "")
                {
                    //If new point is changed, add it in list which is to be sent to client.
                    NewPoint.PointStatus = pointStatus;
                    objGoogle.Markers.Add(NewPoint);
                }
            }
            //Loop through rest of old points to mark them as deleted.
            for (int i = 0; i < objGoogleOld.Markers.Count; i++)
            {
                Marker OldPoint = objGoogleOld.Markers[i];
                OldPoint.PointStatus = "D";
                objGoogle.Markers.Add(OldPoint);
            }

            //*******************Polyline**************************
            for (int i = 0; i < objGoogleNew.Polylines.Count; i++)
            {
                string lineStatus = "";
                GooglePolyline NewLine = objGoogleNew.Polylines[i];
                GooglePolyline OldLine = objGoogleOld.Polylines[NewLine.ID];
                //if old point not found, means this is a new point.
                if (OldLine == null)
                {
                    lineStatus = "N"; //New
                }
                else
                {
                    //If old point found and old not equal to new point, means it's value is changed.
                    if (!OldLine.Equals(NewLine))
                    {
                        lineStatus = "C"; //Changed
                    }
                    
                    //Remove found point from old object. This is to reduce iteration in next loop.
                    objGoogleOld.Polylines.Remove(OldLine.ID);
                   
                }
                if (lineStatus != "")
                {
                    //If new point is changed, add it in list which is to be sent to client.
                    NewLine.LineStatus = lineStatus;
                    objGoogle.Polylines.Add(NewLine);
                }
            }
            //Loop through rest of old points to mark them as deleted.
            for (int i = 0; i < objGoogleOld.Polylines.Count; i++)
            {
                GooglePolyline OldPolyline = objGoogleOld.Polylines[i];
                OldPolyline.LineStatus = "D";
                objGoogle.Polylines.Add(OldPolyline);
            }


            //***********************Polygons***********************
            for (int i = 0; i < objGoogleNew.Polygons.Count; i++)
            {
                string gonStatus = "";
                GooglePolygon NewGon = objGoogleNew.Polygons[i];
                GooglePolygon OldGon = objGoogleOld.Polygons[NewGon.ID];
                //if old point not found, means this is a new point.
                if (OldGon == null)
                {
                    gonStatus = "N"; //New
                }
                else
                {
                    //If old point found and old not equal to new point, means it's value is changed.
                    if (!OldGon.Equals(NewGon))
                    {
                        gonStatus = "C"; //Changed
                    }
                    //Remove found point from old object. This is to reduce iteration in next loop.
                    objGoogleOld.Polygons.Remove(OldGon.ID);
                }
                if (gonStatus != "")
                {
                    //If new point is changed, add it in list which is to be sent to client.
                    NewGon.Status = gonStatus;
                    objGoogle.Polygons.Add(NewGon);
                }
            }
            //Loop through rest of old points to mark them as deleted.
            for (int i = 0; i < objGoogleOld.Polygons.Count; i++)
            {
                GooglePolygon OldPolygon = objGoogleOld.Polygons[i];
                OldPolygon.Status = "D";
                objGoogle.Polygons.Add(OldPolygon);
            }
            

        }

        objGoogle.CenterPoint = objGoogleNew.CenterPoint;
        objGoogle.ZoomLevel = objGoogleNew.ZoomLevel;
        //Save new Google object state in session variable.
        //System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT_OLD"] = objGoogleNew;
        System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT_OLD"] = new GoogleMapObject(objGoogleNew);

        return objGoogle;
    }
}